<?php
class OauthAction  extends WechatAction{
    //获得全局access_token
    public function index(){
        echo "string";
   }

    public function access_token(){
        //请求url地址
        $appId = 'wx23decee761dddfff';
        $appSecret = '2bd6289b4aaaef91aec2eaef7be93d6c';
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
        //初始化curl
        $ch = curl_init($url);
        //3.设置参数
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        //4.调用接口
        $res = curl_exec($ch);
        if(curl_errno($ch)){
            var_dump(curl_error($ch));
        }
        $resArr = json_decode($res,1);
        // var_dump($resArr);
        //5.关闭curl
        curl_close($ch);

         return $resArr['access_token'];
    }
     
    public function sendMessage($openid,$user_card_num,$money,$shop,$firstcolor,$keyword1color,$keyword2color,$keyword3color,$remarkcolor){
       
        $token = $this -> access_token();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
        dump($url); 
        $post_data = array(
            'touser'=>$openid,
            'template_id'=>'ESquXUi09z3gndRKr72w7Of9PNy52JHYQk5ThyGvxs8',
            'url'=>"http://weixin.qq.com/download",
            'data'=>array(
                        'first'=>array('value'=>urlencode("您有新客户，请及时查看！"),'color'=>$firstcolor),
                        'keyword1'=>array('value'=>urlencode($user_card_num),'color'=>$keyword1color),
                        'keyword2'=>array('value'=>urlencode($money."元"),'color'=>$keyword2color),
                        'keyword3'=>array('value'=>urlencode($shop),'color'=>$keyword3color),
                        'remark'=>array('value'=>urlencode('暂无信息'),'color'=>$remarkcolor),
                        )
            );

        //将上面的数组数据转为json格式
        $post_data = json_encode($post_data);
        dump($post_data);
        $res=$this -> http_request($url,urldecode($post_data ));
        // 发送数据，post方式<br>　　　　　　　　　//配置curl请求
        //  // $ch = curl_init($url);//创建curl请求
        // curl_setopt($ch, CURLOPT_URL,$url); //设置发送数据的网址
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值，0，直接显示
        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
        // curl_setopt($ch, CURLOPT_POST, 1);　　//post方法请求
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post请求发送的数据包
        // //接收执行返回的数据
        // $data = curl_exec($ch);
        // //关闭句柄
        // curl_close($ch);
        // $data = json_decode($data,true); //将json数据转成数组
        // // dump($token); 
        return $res;
    }
    
    public function sendMes($openid,$content,$user_card_num,$firstcolor,$keyword1color,$keyword2color,$remarkcolor){
         
        $token = $this -> access_token();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
        dump($url);
        $post_data = array(
            'touser'=>$openid,
            'template_id'=>'gURDg4jpxAJxO2XsxdzYszMOhKvRh8lNe6WhxFOF-H4',
            'url'=>"http://weixin.qq.com/download",
            'data'=>array(
                'first'=>array('value'=>urlencode("您新的消息！"),'color'=>$firstcolor),
                'keyword1'=>array('value'=>urlencode($user_card_num),'color'=>$keyword1color),
                'keyword2'=>array('value'=>urlencode($content),'color'=>$keyword2color),
             
                'remark'=>array('value'=>urlencode('暂无信息'),'color'=>$remarkcolor),
            )
        );
    
        //将上面的数组数据转为json格式
        $post_data = json_encode($post_data);
        dump($post_data);
        $res=$this -> http_request($url,urldecode($post_data ));
        // 发送数据，post方式<br>　　　　　　　　　//配置curl请求
        //  // $ch = curl_init($url);//创建curl请求
        // curl_setopt($ch, CURLOPT_URL,$url); //设置发送数据的网址
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值，0，直接显示
        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
        // curl_setopt($ch, CURLOPT_POST, 1);　　//post方法请求
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post请求发送的数据包
        // //接收执行返回的数据
        // $data = curl_exec($ch);
        // //关闭句柄
        // curl_close($ch);
        // $data = json_decode($data,true); //将json数据转成数组
        // // dump($token);
        return $res;
    }

    public function http_request($url,$data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // 我们在POST数据哦！
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
}

    






  private function send($mobile,$content){
        
    
        
    
        
        /*--------------------------------
         功能:        上海助通短信PHP HTTP接口 发送短信
         auther   henry
         说明:        发送传递的参数
         http://www.ztsms.cn:8800/sendXSms.do?username=用户名&password=密码&mobile=手机号码&content=内容&dstime=&productid=产品ID&xh=留空
         --------------------------------*/
        $username = 'shjs';     //用户账号
        $password = 'r2zR064g';     //密码
//         $mobile   = '13916888888';   //号码
//         $content = '测试PHP HTTP接口';       //内容
        $content=iconv("UTF-8", "UTF-8", $content);
        $dstime = '';       //为空代表立即发送  如果加了时间代表定时发送  精确到秒
        $productid =  95533;        //内容
        $xh = '';       //留空
        
        $url="http://www.ztsms.cn:8800/sendXSms.do?username=$username&password=$password&mobile=$mobile&content=$content&dstime=&productid=$productid&xh=";
       
//        echo  $url;
    $res=$this->Get($url);
   
        
//         if(function_exists('file_get_contents'))
//         {
//             $file_contents = file_get_contents($url);
//         }
//         else
//         {
//             $ch = curl_init();
//             $timeout = 5;
//             curl_setopt ($ch, CURLOPT_URL, $url);
//             curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
//             curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//             $file_contents = curl_exec($ch);
//             curl_close($ch);
//         }
//         return $file_contents;
        
        return $res;
    }
 public function sendSms($tel,$user_card_num,$money,$shop){
      
        
        $data ="您好，您的店铺有一笔新的消费信息注意查看";
        
          $this->send($tel,$data);

}

    public function openid(){
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx23decee761dddfff&secret=2bd6289b4aaaef91aec2eaef7be93d6c";
        $token=$this->Get($url);
        $token=json_decode($token,true);    
        $url2="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx23decee761dddfff&redirect_uri=http://www.nleaves.com/qinqin/wechat/business/index&response_type=code&scope=snsapi_base#wechat_redirect";
        // $token1=$this->Get($url2);
        // file_get_contents($url);
        // echo 111;exit;
        header('Location:'.$url2); 
    }
    
        
        
        
        
    

















    }
   
